import java.util.HashMap;
import java.util.Map;

// 状态机类
class StateMachine {
    // 当前状态
    private String currentState;
    // 存储各个状态及对应的触发事件和转移状态的映射关系
    private Map<String, Map<String, String>> states;

    // 构造方法，初始化状态机的状态及转移规则
    public StateMachine() {
        states = new HashMap<>();

        // 已创建状态的转移规则
        Map<String, String> createdTransitions = new HashMap<>();
        createdTransitions.put("pay", "paid");
        states.put("created", createdTransitions);

        // 已支付状态的转移规则
        Map<String, String> paidTransitions = new HashMap<>();
        paidTransitions.put("ship", "shipped");
        states.put("paid", paidTransitions);

        // 已发货状态的转移规则
        Map<String, String> shippedTransitions = new HashMap<>();
        shippedTransitions.put("sign", "signed");
        states.put("shipped", shippedTransitions);

        currentState = "created";
    }

    // 触发事件的方法，根据传入的事件来改变状态
    public void triggerEvent(String event) {
        Map<String, String> currentStateTransitions = states.get(currentState);
        if (currentStateTransitions!= null && currentStateTransitions.containsKey(event)) {
            currentState = currentStateTransitions.get(event);
            System.out.println("状态已更新为: " + currentState);
        } else {
            System.out.println("无效事件 " + event + " 在当前状态 " + currentState);
        }
    }

    public static void main(String[] args) {
        StateMachine orderStateMachine = new StateMachine();
        orderStateMachine.triggerEvent("pay");
        orderStateMachine.triggerEvent("ship");
        orderStateMachine.triggerEvent("sign");
    }
}